package com.ow.dva.module.order.controller;


import com.ow.dva.config.constant.UserConstant;
import com.ow.dva.config.shiro.ShiroAccount;
import com.ow.dva.module.account.entity.Account;
import com.ow.dva.module.base.controller.BaseController;
import com.ow.dva.module.base.entity.json.RT;
import com.ow.dva.module.base.entity.param.DvaPage;
import com.ow.dva.module.order.entity.Order;
import com.ow.dva.module.order.service.OrderService;
import com.ow.dva.module.park.service.RoadService;
import com.ow.dva.module.passingRecord.entity.PassingRecord;
import com.ow.dva.util.DateOfMonth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.ow.dva.util.GetReqJson.getRequestJsonString;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author YanZengBao
 * @version V1.0
 * @since 2020-02-19
 */
@Controller
@Api(tags = "订单表 相关控制器")
@RequestMapping("/order")
public class OrderController extends BaseController {

    /**
     * 分页获取订单表数据列表信息
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "分页获取", notes = "批量获取订单表信息列表，分页，可携带条件")
    @RequiresPermissions("order:page")
    @GetMapping(value = "/page")
    public String page(DvaPage<Order> dvaPage, @RequestParam Map<String, Object> param, Model model) {

        DvaPage<Order> page = orderService.page(dvaPage, param);
        model.addAttribute("page", page);
        model.addAttribute("param", param);
        return "order/list";
    }

    /**
     * 进入订单表添加页面。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入添加页面", notes = "进入订单表添加页面")
    @RequiresPermissions("order:add")
    @GetMapping(value = "/add")
    public String add() {

        return "order/add";
    }

    /**
     * 添加订单表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "添加", notes = "添加单个订单表信息")
    @RequiresPermissions("order:add")
    @PostMapping("/save")
    @ResponseBody
    public RT save(Order order) {

        return orderService.save(order) ? RT.ok() : RT.error(RT.ERROR_0101);
    }

    /**
     * 进入订单表编辑页面。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入编辑页面", notes = "进入订单表编辑页面")
    @RequiresPermissions("order:edit")
    @GetMapping(value = "/edit")
    public String edit(String id, Model model) {

        model.addAttribute("data", orderService.getById(id));

        return "order/edit";
    }

    /**
     * 修改订单表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "修改", notes = "修改单个订单表信息")
    @RequiresPermissions("order:edit")
    @PostMapping("/update")
    @ResponseBody
    public RT update(Order order) {

        return orderService.updateById(order) ? RT.ok() : RT.error(1);
    }

    /**
     * 删除单个订单表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "删除", notes = "删除单个订单表信息")
    @RequiresPermissions("order:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RT delete(@ApiParam(name = "要删除的订单表ID") String id) {

        return orderService.removeById(id) ? RT.ok() : RT.error(1);
    }

    /**
     * 批量删除多个订单表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "批量删除", notes = "批量删除多个订单表信息")
    @RequiresPermissions("order:delete")
    @PostMapping("/deleteByIds")
    @ResponseBody
    public RT deleteByIds(@ApiParam(name = "要删除的订单表ID List") @RequestParam List<String> ids) {

        return orderService.removeByIds(ids) ? RT.ok() : RT.error(1);
    }


    @RequestMapping(value = "/selectMoneyByParkId")
    @ResponseBody
    public RT selectMoneyByParkId(String parkId) {

        List<Order> orders = new ArrayList<>();

        if (parkId.equals("") || parkId == null) {
            orders = orderService.getAllTodayOrders();
        } else {
            orders = orderService.getAllTodayOrdersByParkId(parkId);
        }

        double money = 0.00;
        for (Order order : orders) {
            money += order.getReallyMoney();
        }
        return RT.ok(money);
    }

    @RequestMapping(value = "/selectByInfoId")
    @ResponseBody
    public RT selectByInfoId(String parkId) {
        List<Order> orderList = new ArrayList<>();

        if (parkId.equals("") || parkId == null) {
            orderList = orderService.getAllTodayOrders();
        } else {
            orderList = orderService.getAllTodayOrdersByParkId(parkId);
        }

        List<Order> orders = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Order order = new Order();
            order.setPayType(i);
            if (i == 0) {
                order.setPayTypeS("支付宝");
            } else if (i == 1) {
                order.setPayTypeS("微信");
            } else if (i == 2) {
                order.setPayTypeS("余额");
            } else if (i == 3) {
                order.setPayTypeS("现金");
            } else if (i == 4) {
                order.setPayTypeS("场内支付");
            }
            order.setCount(0);
            orders.add(order);
        }
        if (orderList.size() != 0) {
            for (Order o : orderList) {
                if (o != null) {
                    if (o.getPayType() == 1) {
                        orders.get(0).setCount(orders.get(0).getCount() + 1);
                    } else if (o.getPayType() == 2) {
                        orders.get(1).setCount(orders.get(1).getCount() + 1);
                    } else if (o.getPayType() == 3) {
                        orders.get(2).setCount(orders.get(2).getCount() + 1);
                    } else if (o.getPayType() == 4) {
                        orders.get(3).setCount(orders.get(3).getCount() + 1);
                    } else if (o.getPayType() == 5) {
                        orders.get(4).setCount(orders.get(4).getCount() + 1);
                    }
                }
            }
        }

        return RT.ok(orders);
    }

    @RequestMapping(value = "/dateByInfoId")
    @ResponseBody
    public RT dateByInfoId(String parkId) {

        List<Order> orderList = new ArrayList<>();
        List<Order> orders = new ArrayList<>();
        if (parkId.equals("") || parkId == null) {
            orders = orderService.getAllTodayOrders();
        } else {
            orders = orderService.getAllTodayOrdersByParkId(parkId);
        }
        for (int i = 0; i < 24; i++) {
            Order order = new Order();
            order.setReallyMoney(0.00);
            for (Order o : orders) {
                int hour = o.getCreateTime().getHours();
                if (hour == i) {
                    order.setReallyMoney(order.getReallyMoney() + o.getReallyMoney());
                }
            }
            orderList.add(order);
        }

        return RT.ok(orderList);
    }

    @RequestMapping(value = "/orderByInfoId")
    @ResponseBody
    public List<Order> orderByInfoId(String parkId, String mode, String date) throws Exception {

        List<Order> orders = new ArrayList<>();
        List<Order> orderList = new ArrayList<>();
        Integer counts = 0;//订单总数
        double moneys = 0.00;//收费总额
        Integer completeCount = 0;//已完成数量
        double completeMoney = 0.00;//已完成金额

        Map map = new HashMap();
        Map m = new HashMap();
        if (mode.equals("day")) {//某日订单数
            if (parkId.equals("") || parkId == null) {
                map.put("createTime", date);
                orders = orderService.someDayOrders(map);
            } else {
                map.put("parkId", parkId);
                map.put("createTime", date);
                orders = orderService.someDayOrdersByPark(map);
            }
        } else if (mode.equals("halfMonth")) {//近十五日订单数
            if (parkId.equals("") || parkId == null) {
                map.put("createTime", date);
                orders = orderService.halfMonthOrders(map);
            } else {
                map.put("parkId", parkId);
                map.put("createTime", date);
                orders = orderService.halfMonthOrdersByPark(map);
            }
        } else if (mode.equals("month")) {//某月订单数
            if (parkId.equals("") || parkId == null) {
                map.put("createTime", date);
                orders = orderService.oneMonthOrders(map);
            } else {
                map.put("parkId", parkId);
                map.put("createTime", date);
                orders = orderService.oneMonthOrdersByPark(map);
            }
        } else if (mode.equals("year")) {//某年度订单数
            if (parkId.equals("") || parkId == null) {
                map.put("createTime", date);
                orders = orderService.oneYearOrders(map);
            } else {
                map.put("parkId", parkId);
                map.put("createTime", date);
                orders = orderService.oneYearOrdersByPark(map);
            }
        } else {//当日订单数
            if (parkId.equals("") || parkId == null) {
                orders = orderService.getAllTodayOrders1();
            } else {
                orders = orderService.getAllTodayOrdersByParkId1(parkId);
            }
        }

        counts = orders.size();

        for (int i = 0; i < orders.size(); i++) {

            Order order = orders.get(i);

            moneys = moneys + order.getReallyMoney();
            if (order.getOrderStatus() == 1) {
                completeCount = completeCount + 1;
                completeMoney = completeMoney + order.getReallyMoney();
            }
        }

        m.put("counts", counts);
        m.put("moneys", moneys);
        m.put("completeCount", completeCount);
        m.put("completeMoney", completeMoney);
        m.put("date", date);

        if (mode.equals("day")) {//某日订单数
            orderList = todays(orders, m);
        } else if (mode.equals("halfMonth")) {//近十五日订单数
            orderList = halfMonths(orders, m);
        } else if (mode.equals("month")) {//某月订单数
            orderList = months(orders, m);
        } else if (mode.equals("year")) {//某年度订单数
            orderList = years(orders, m);
        } else {//当日订单数
            orderList = todays(orders, m);
        }


        return orderList;
    }

    public List<Order> todays(List<Order> orders, Map map) {

        Integer counts = Integer.parseInt(map.get("counts").toString());
        double moneys = Double.parseDouble(map.get("moneys").toString());
        Integer completeCount = Integer.parseInt(map.get("completeCount").toString());
        double completeMoney = Double.parseDouble(map.get("completeMoney").toString());
        String date = map.get("date").toString();
        List<Order> orderList = new ArrayList<>();
        List<Order> arrayList = new ArrayList<>();

        for (int i = 0; i < 24; i++) {
            double reallyMoney = 0.00;
            double allReallyMoneys = 0.00;
            Integer singleCounts = 0;
            Integer allCompleteCount = 0;
            Order order = new Order();
            order.setMoneys(moneys);
            order.setCompleteMoney(completeMoney);
            order.setCounts(counts);
            order.setCompleteCount(completeCount);
            order.setDate(date);
            order.setAllReallyMoneys(allReallyMoneys);
            order.setReallyMoney(reallyMoney);
            for (Order o : orders) {
                int hour = o.getCreateTime().getHours();
                if (hour == i) {
                    allReallyMoneys = o.getReallyMoney() + allReallyMoneys;
                    order.setAllReallyMoneys(allReallyMoneys);
                    allCompleteCount = allCompleteCount + 1;
                    order.setAllCompleteCount(allCompleteCount);
                    if (o.getOrderStatus() == 1) {
                        reallyMoney = o.getReallyMoney() + reallyMoney;
                        order.setReallyMoney(reallyMoney);
                        singleCounts = singleCounts+1;
                        order.setSingleCounts(singleCounts);
                    }

                    orderList.add(o);
                }
            }
            arrayList.add(order);
        }

        return arrayList;
    }

    public List<Order> halfMonths(List<Order> orders, Map map) {

        Integer counts = Integer.parseInt(map.get("counts").toString());
        double moneys = Double.parseDouble(map.get("moneys").toString());
        Integer completeCount = Integer.parseInt(map.get("completeCount").toString());
        double completeMoney = Double.parseDouble(map.get("completeMoney").toString());
        String date = map.get("date").toString();
        List<Order> orderList = new ArrayList<>();
        List<Order> arrayList = new ArrayList<>();
        DateOfMonth dates = new DateOfMonth();
        for (int i = 0; i < 15; i++) {
            double reallyMoney = 0.00;
            double allReallyMoneys = 0.00;
            Integer singleCounts = 0;
            Integer allCompleteCount = 0;
            Order order = new Order();
            order.setMoneys(moneys);
            order.setCompleteMoney(completeMoney);
            order.setCounts(counts);
            order.setCompleteCount(completeCount);
            order.setDate(date);
            order.setAllReallyMoneys(allReallyMoneys);
            order.setReallyMoney(reallyMoney);
            for (Order o : orders) {
                int weight = dates.getVerify(i);
                int day = o.getCreateTime().getDate();
                if (day == weight) {
                    allReallyMoneys = o.getReallyMoney() + allReallyMoneys;
                    order.setAllReallyMoneys(allReallyMoneys);
                    allCompleteCount = allCompleteCount + 1;
                    order.setAllCompleteCount(allCompleteCount);
                    if (o.getOrderStatus() == 1) {
                        reallyMoney = o.getReallyMoney() + reallyMoney;
                        order.setReallyMoney(reallyMoney);
                        singleCounts = singleCounts+1;
                        order.setSingleCounts(singleCounts);
                    }

                    orderList.add(o);
                }
            }
            arrayList.add(order);
        }

        Collections.reverse(arrayList);

        return arrayList;
    }



    public List<Order> months(List<Order> orders, Map map) {

        Integer counts = Integer.parseInt(map.get("counts").toString());
        double moneys = Double.parseDouble(map.get("moneys").toString());
        Integer completeCount = Integer.parseInt(map.get("completeCount").toString());
        double completeMoney = Double.parseDouble(map.get("completeMoney").toString());
        String date = map.get("date").toString();
        List<Order> orderList = new ArrayList<>();
        List<Order> arrayList = new ArrayList<>();

//        当月有多少天
        DateOfMonth dates = new DateOfMonth();
        Integer dateOfMonth = dates.getDeteOfMonth();

        for (int i = 0; i < dateOfMonth; i++) {
            double reallyMoney = 0.00;
            double allReallyMoneys = 0.00;
            Integer singleCounts = 0;
            Integer allCompleteCount = 0;
            Order order = new Order();
            order.setMoneys(moneys);
            order.setCompleteMoney(completeMoney);
            order.setCounts(counts);
            order.setCompleteCount(completeCount);
            order.setDate(date);
            order.setAllReallyMoneys(allReallyMoneys);
            order.setReallyMoney(reallyMoney);
            order.setMonths(dateOfMonth);
            for (Order o : orders) {
                int day = o.getCreateTime().getDate();
                if (day == i+1) {
                    allReallyMoneys = o.getReallyMoney() + allReallyMoneys;
                    order.setAllReallyMoneys(allReallyMoneys);
                    allCompleteCount = allCompleteCount + 1;
                    order.setAllCompleteCount(allCompleteCount);
                    if (o.getOrderStatus() == 1) {
                        reallyMoney = o.getReallyMoney() + reallyMoney;
                        order.setReallyMoney(reallyMoney);
                        singleCounts = singleCounts+1;
                        order.setSingleCounts(singleCounts);
                    }

                    orderList.add(o);
                }
            }
            arrayList.add(order);
        }

        return arrayList;
    }

    public List<Order> years(List<Order> orders, Map map) {

        Integer counts = Integer.parseInt(map.get("counts").toString());
        double moneys = Double.parseDouble(map.get("moneys").toString());
        Integer completeCount = Integer.parseInt(map.get("completeCount").toString());
        double completeMoney = Double.parseDouble(map.get("completeMoney").toString());
        String date = map.get("date").toString();
        List<Order> orderList = new ArrayList<>();
        List<Order> arrayList = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            double reallyMoney = 0.00;
            double allReallyMoneys = 0.00;
            Integer singleCounts = 0;
            Integer allCompleteCount = 0;
            Order order = new Order();
            order.setMoneys(moneys);
            order.setCompleteMoney(completeMoney);
            order.setCounts(counts);
            order.setCompleteCount(completeCount);
            order.setDate(date);
            order.setAllReallyMoneys(allReallyMoneys);
            order.setReallyMoney(reallyMoney);;
            for (Order o : orders) {
                int month = o.getCreateTime().getMonth() + 1;
                if (month == i) {
                    allReallyMoneys = o.getReallyMoney() + allReallyMoneys;
                    order.setAllReallyMoneys(allReallyMoneys);
                    allCompleteCount = allCompleteCount + 1;
                    order.setAllCompleteCount(allCompleteCount);
                    if (o.getOrderStatus() == 1) {
                        reallyMoney = o.getReallyMoney() + reallyMoney;
                        order.setReallyMoney(reallyMoney);
                        singleCounts = singleCounts+1;
                        order.setSingleCounts(singleCounts);
                    }

                    orderList.add(o);
                }
            }
            arrayList.add(order);
        }

        return arrayList;
    }



    @Resource
    OrderService orderService;
}
